package cn.ljl.basic.controller;

import cn.ljl.basic.util.AjaxResult;
import cn.ljl.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    //文件上传接口
    @PostMapping
    public AjaxResult uploadFile(@RequestPart MultipartFile file){
        try {
            String oName = file.getOriginalFilename();
            String exename = oName.substring(oName.lastIndexOf(".") + 1);//调用工具类
            String fileName = FastDfsUtils.upload(file.getBytes(), exename);
            return AjaxResult.me().setObj(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }
    }
    //删除接口
    @DeleteMapping
    public AjaxResult deleteFile(@RequestParam("path")String path){
        try {
            path= path.substring(1);
            String groupName = path.substring(0, path.indexOf("/"));
            String fileName =path.substring(path.indexOf("/")+1);
            FastDfsUtils.delete(groupName,fileName);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

}
